package com.team5101.product.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team5101.constant.RabbitMQ;
import com.team5101.dto.TProductDto;
import com.team5101.base.BaseServiceImpl;
import com.team5101.base.IBaseDao;
import com.team5101.entity.TProduct;
import com.team5101.entity.TProductDesc;
import com.team5101.mapper.TProductDescMapper;
import com.team5101.mapper.TProductMapper;
import com.team5101.product.api.IProductService;
import com.team5101.vo.ResultBean;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;


//@Component
@Service
public class ProductServiceImpl extends BaseServiceImpl<TProduct> implements IProductService {

    @Autowired
    private TProductMapper mapper;

    @Autowired
    private TProductDescMapper tProductDescMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public IBaseDao<TProduct> getBaseDao() {
        return mapper;
    }

    @Override
    public int deleteByPrimaryKey(Long sId) {
       tProductDescMapper.delByPid(sId);
        return mapper.deleteByPrimaryKey(sId);
    }

    @Override
    public int insert(TProduct record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(TProduct record) {
        return mapper.insertSelective(record);
    }

    @Override
    public TProduct selectByPrimaryKey(Long sId) {
        return mapper.selectByPrimaryKey(sId);
    }

    @Override
    public int updateByPrimaryKeySelective(TProduct record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(TProduct record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public List<TProductDto> getAll() {
        return mapper.getAll();
    }

    @Override
    public ResultBean getProByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TProductDto> products = getAll();
        PageInfo<TProductDto> pageInfo = new PageInfo<TProductDto>(products,5);
        return ResultBean.success(pageInfo,"分页数据");
    }

    @Override
    public ResultBean addProduct(TProductDto tProductDto) {
        Byte b = 1;
        TProduct tProduct = new TProduct();
        tProduct.setCreateTime(new Date());
        tProduct.setCreateUser(1L);
        tProduct.setFlag(b);
        tProduct.setPimage(tProductDto.getPimage());
        tProduct.setPname(tProductDto.getPname());
        tProduct.setPrice(tProductDto.getPrice());
        tProduct.setSalePrice(tProductDto.getSalePrice());
        tProduct.setStatus(b);
        tProduct.setUpdateTime(new Date());
        tProduct.setUpdateUser(1L);
        int i = mapper.insertSelective(tProduct);
        TProductDesc tProductDesc = new TProductDesc();
        tProductDesc.setPid(tProduct.getPid());
        tProductDesc.setPdesc(tProductDto.getPdesc());
        tProductDesc.setCreateTime(new Date());
        tProductDesc.setCreateUser(1L);
        tProductDesc.setUpdateUser(1L);
        tProductDesc.setFlag(b);
        tProductDesc.setUpdateTime(new Date());
        int i1 = tProductDescMapper.insertSelective(tProductDesc);
        rabbitTemplate.convertAndSend(RabbitMQ.PRODUCT_ADD_EXCHANGE,"product.add",tProduct);
        if (i==1&&i1==1){
            return ResultBean.success("添加成功");
        }
        return ResultBean.error("添加失败");
    }

    @Override
    public TProductDto selectOne(Long pid) {
        return mapper.selectOne(pid);
    }

    @Override
    public int updateByPid(TProductDesc td) {
        return tProductDescMapper.updateByPid(td);
    }
}
